#!/usr/bin/env python
from google.appengine.ext import db
from google.appengine.api import mail
import CourseM

def addInvite(googleID, course):
    owner = GeneralM.getEmail()
    googleID=googleID.strip()
    if not course:
        return 'error: course not found'
    elif not owner:
        return 'error: user is not logged in'
    elif googleID == '':
        return 'partner name cannot be empty'
    elif db.Query(Ownership).filter('owner =', googleID).filter('course =', course).count() > 0:
        return 'Ownership already exists'
    else:
        ownership = Ownership(owner = googleID, course = course, active=True)
        ownership.put()
        return ownership

def sendInvite(googleID, fullName, courseName, ownershipKey):
    mail.send_mail(sender="StudyWise <noreply@studywise.com>",
              to= fullName + '<' + googleID + '>',
              subject="You have been invited to share a course on studyWise",
              body='Dear ' + fullName + ',\n' + 
              'You have been invited to share the course:\n' +
              courseName + '\nPlease click the link to add this course:\n' +
              'study-wise.appspot.com/invite?key=' + unicode(ownershipKey))

class Ownership(db.Model):
    course = db.ReferenceProperty(CourseM.Course)
    owner = db.StringProperty()
    active = db.BooleanProperty()
    
import GeneralM    